package com.dongjiantong.app.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.biz.model.FarmStock;
import com.dongjiantong.biz.service.FarmStockService;
import com.dongjiantong.common.exceptions.HttpCode;

import com.dongjiantong.common.exceptions.OPServerException;

import com.dongjiantong.common.model.Response;
import com.dongjiantong.common.web.sso.LoginInfo;
import com.dongjiantong.common.web.util.UserUtil;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.dongjiantong.sys.model.SysLabelModel;
import com.dongjiantong.user.model.*;
import com.dongjiantong.user.model.dtos.FarmStockDto;
import com.dongjiantong.user.model.dtos.FarmsDto;
import com.dongjiantong.user.model.dtos.VillageFarm;
import com.dongjiantong.user.service.*;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.*;

/**
 * Created by ThinkPad on 2018/1/4.
 */
@Slf4j
@OpenBean
public class FarmController {

    @Reference(version = "1.0.0")
    private FarmsService farmsService;
    @Reference(version = "1.0.0")
    private RoleAppUserService roleAppUserService;
    @Reference(version = "1.0.0")
    private FarmsUserService farmsUserService;
    @Reference(version = "1.0.0")
    private MzUserService mzUserService;
    @Reference(version = "1.0.0")
    private FarmStockService farmStockService;

    /**
     * 根据养殖场ids查询养殖场
     *
     * @return
     */
    @OpenMethod(key = "app.user.selectUserFarms",
            paramNames = {},
            httpMethods = RequestMethod.GET
    )
    public Response<List<FarmsDto>> selectUserFarms() {

        LoginInfo loginInfo = UserUtil.getCurrentUser();
        List<Long> list = loginInfo.getBreedFactoryIds();
        Map map = new HashMap();

        map.put("farmIds", list);
        List<FarmsDto> farmsList = farmsService.selectFarmStock(map);
        return Response.ok(farmsList);

    }

    @OpenMethod(key = "app.Farm.selectLikeReFarm",
            paramNames = {"pageNum", "pageSize", "name", "status", "villageId"},
            httpMethods = RequestMethod.GET
    )
    public Response<PageInfo> selectLikeReFarm(Integer pageNum, Integer pageSize, String name, Integer status, Integer villageId) {

        LoginInfo loginInfo = UserUtil.getCurrentUser();
        List<Long> list = loginInfo.getBreedFactoryIds();
        Map map = new HashMap();

        map.put("farmIds", list);
        PageInfo<String> villageList = farmsService.selectFarmVillage(pageNum, pageSize, map);
        map.put("name", name);
        List<VillageFarm> villageFarmList = new ArrayList<>();
        for (String village : villageList.getList()) {
            VillageFarm villageFarm = new VillageFarm();
            map.put("village", village);
            map.put("status", status);
            map.put("villageId", villageId);
            List<FarmsDto> farmsDtoList = farmsService.selectFarmStock(map);
            villageFarm.setData(farmsDtoList);
            villageFarm.setKey(village);
            villageFarmList.add(villageFarm);

        }
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList(villageFarmList);
        pageInfo.setTotal(villageList.getTotal());
        return Response.ok(pageInfo);


//        Map<String, List<FarmsDto>> resultMap= new HashMap<String, List<FarmsDto>>();
//
//        for(int i=0;i<farmsList.size();i++){
//            FarmsDto farmsDto = farmsList.get(i);
//            if(resultMap.containsKey(farmsDto.getAddress())){
//                resultMap.get(farmsDto.getAddress()).add(farmsDto);
//            }else{
//                List<FarmsDto> list1 = new ArrayList<FarmsDto>();
//                list1.add(farmsDto);
//                resultMap.put(farmsDto.getAddress(),list1);
//            }
//        }
//        return Response.ok(farmsList);

    }


    /**
     * 模糊搜索养殖场
     *
     * @return
     */
    @OpenMethod(key = "app.Farm.selectLikeFarm",
            paramNames = {"pageNum", "pageSize", "name"},
            httpMethods = RequestMethod.GET
    )
    public Response<PageInfo> selectLikeFarm(Integer pageNum, Integer pageSize, String name) {

        LoginInfo loginInfo = UserUtil.getCurrentUser();
        List<Long> list = loginInfo.getBreedFactoryIds();
        Map map = new HashMap();
        map.put("farmIds", list);
        map.put("name", name);

        return Response.ok(farmsService.selectFarmStock(pageNum, pageSize, map));


//        Map<String, List<FarmsDto>> resultMap= new HashMap<String, List<FarmsDto>>();
//
//        for(int i=0;i<farmsList.size();i++){
//            FarmsDto farmsDto = farmsList.get(i);
//            if(resultMap.containsKey(farmsDto.getAddress())){
//                resultMap.get(farmsDto.getAddress()).add(farmsDto);
//            }else{
//                List<FarmsDto> list1 = new ArrayList<FarmsDto>();
//                list1.add(farmsDto);
//                resultMap.put(farmsDto.getAddress(),list1);
//            }
//        }
//        return Response.ok(farmsList);

    }

    /**
     * 根据养殖场用户id查看养殖场详情
     *
     * @param userId
     * @return
     */
    @OpenMethod(key = "app.BreedUser.selectUserFarms",
            paramNames = {"userId"},
            httpMethods = RequestMethod.GET
    )
    public Response<FarmStockDto> aa(Long userId) {

        Farms farms = farmsUserService.getFarmByUserId(userId);
        if (farms == null) {
            throw new OPServerException(HttpCode.NOT_FOUND, "该用户暂未关联养殖场");
        }
        FarmStock farmStock = farmStockService.selectByFarmID(farms.getId());
        FarmStockDto farmStockDto = new FarmStockDto();
        farmStockDto.setFarms(farms);
        farmStockDto.setFarmStock(farmStock);
        return Response.ok(farmStockDto);
    }


    /**
     * 养殖端--查询屠宰场
     *
     * @return
     */
    @OpenMethod(key = "app.Farm.selectFarmByFarmType",
            paramNames = {"provinceId", "cityId", "regionId", "farmType"},
            httpMethods = RequestMethod.GET
    )
    public Response<List> selectFarmByFarmType(Integer provinceId, Integer cityId, Integer regionId, Integer farmType) {

        Farms farms = new Farms();
        farms.setProvinceId(provinceId);
        farms.setCityId(cityId);
        farms.setRegionId(regionId);
        farms.setFarmsType(farmType);
        return Response.ok(farmsService.select(farms));
    }

    /**
     * 官方兽医管辖范围内的场
     *
     * @param userId   当前登录人id
     * @param farmType 1养殖场 2散养场 3 屠宰场
     * @return
     */
    @OpenMethod(key = "app.user.selectGovUserFarms",
            paramNames = {"userId", "farmType"},
            httpMethods = RequestMethod.GET

    )
    public Response<List> selectUserFarms(Long userId, Integer farmType) {

        Map map = new HashMap();
        map.put("farmType", farmType);
        map.put("userId", userId);
        List<Farms> farmsList = farmsService.selectByMap(map);
        List list = new ArrayList();
        for (Farms farms : farmsList) {
            SysLabelModel sysLabelModel = new SysLabelModel();
            sysLabelModel.setValue(Math.toIntExact(farms.getId()));
            sysLabelModel.setLabel(farms.getFarmsname());
            list.add(sysLabelModel);
        }
        return Response.ok(list);
    }


    /** 猪牛羊家禽存栏数
     * @return
     */
    @OpenMethod(key = "app.Farm.selectStockByType",
            httpMethods = RequestMethod.GET
    )
    public Response<Map> selectStockByType() throws Exception{

        Map result = new HashMap();
        LoginInfo loginInfo = UserUtil.getCurrentUser();
        List<Long> farmIdList = loginInfo.getBreedFactoryIds();
        Map map = new HashMap();
        map.put("farmIds", farmIdList);
        List<FarmsDto> farmsDtoList = farmsService.selectFarmStock(map);
        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;
        for (FarmsDto farmsDto : farmsDtoList) {
            if (farmsDto.getReal_stock()!=null){
                switch (farmsDto.getAnimalTwoType()) {
                    case 101:
                        a = a + farmsDto.getReal_stock();
                        break;
                    case 102:
                        b = b + farmsDto.getReal_stock();
                        break;
                    case 103:
                        c = c + farmsDto.getReal_stock();
                        break;
                    default:
                        d = d + farmsDto.getReal_stock();
                        break;
                }
            }


        }

        result.put("pig", a);
        result.put("cow", b);
        result.put("sheep", c);
        result.put("poultry", d);
        return Response.ok(result);
    }


}